package com.ylyq.ylxx.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ylyq.ylxx.constants.AccountConstant;
import com.ylyq.ylxx.constants.RabbitConstant;
import com.ylyq.ylxx.context.FilterContextHandler;
import com.ylyq.ylxx.entity.*;
import com.ylyq.ylxx.lingo.ILingoService;
import com.ylyq.ylxx.lingo.ResultCore;
import com.ylyq.ylxx.mapper.*;
import com.ylyq.ylxx.model.ResponseData;
import com.ylyq.ylxx.model.bean.CoreResultBean;
import com.ylyq.ylxx.model.bean.OceanengineCreativeInfo;
import com.ylyq.ylxx.service.ICoreService;
import com.ylyq.ylxx.service.IYlyqCityService;
import com.ylyq.ylxx.util.HttpClientUtils;
import com.ylyq.ylxx.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @ClassName CoreServiceImpl
 * @Description TODO
 * @Author xiaweichao
 * @Date 2020/7/21 14:05
 * @Version 1.0
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CoreServiceImpl implements ICoreService {
    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
    @Autowired
    private ILingoService iLingoService;
    @Autowired
    private CoreMapper coreMapper;
    @Autowired
    private YlyqCoreAgeMapper ylyqCoreAgeMapper;
    @Autowired
    private YlyqCoreAreaMapper ylyqCoreAreaMapper;
    @Autowired
    private YlyqCoreSexMapper ylyqCoreSexMapper;
    @Autowired
    private YlyqCoreTagMapper ylyqCoreTagMapper;
    @Autowired
    private IYlyqCityService iYlyqCityService;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public void refreshCore() {
        ylyqCoreTagMapper.deleteAll(null);
        redisUtil.del("ylyqCoreTag");
        List<YlyqCoreAge> ylyqCoreAges = ylyqCoreAgeMapper.selectList(null);
        List<YlyqCoreArea> ylyqCoreAreas = ylyqCoreAreaMapper.selectList(null);
        List<YlyqCoreSex> ylyqCoreSexes = ylyqCoreSexMapper.selectList(null);

//人群包各维度得分 = 0.4 * 年龄得分 + 0.2 * 性别得分 + 0.4 * 地域得分
        List<YlyqCoreTag> ylyqCoreTagList = new ArrayList<>();
        for (YlyqCoreAge age : ylyqCoreAges) {
            for (YlyqCoreArea area : ylyqCoreAreas) {
                for (YlyqCoreSex sex : ylyqCoreSexes) {
                        YlyqCoreTag tag = new YlyqCoreTag();
                        tag.setPlanName( age.getAgeFlag() + "#" + area.getAreaFlag() + "#" + sex.getSexFlag());
                        tag.setBd(0.4 * age.getBd() + 0.4 * area.getBd() + 0.2 * sex.getBd() );
                        tag.setDx(0.4 * age.getDx() + 0.4 * area.getDx() + 0.2 * sex.getDx() );
                        tag.setZpl(0.4 * age.getZpl() + 0.4 * area.getZpl() + 0.2 * sex.getZpl() );
                        tag.setXl(0.4 * age.getXl() + 0.4 * area.getXl() + 0.2 * sex.getXl() );
                        tag.setKk(0.4 * age.getKk() + 0.4 * area.getKk() + 0.2 * sex.getKk() );
                        tag.setCz(0.4 * age.getCz() + 0.4 * area.getCz() + 0.2 * sex.getCz() );
                        tag.setBhy(0.4 * age.getBhy() + 0.4 * area.getBhy() + 0.2 * sex.getBhy());
                        tag.setX(0.4 * age.getX() + 0.4 * area.getX() + 0.2 * sex.getX() );
                        tag.setMs(0.4 * age.getMs() + 0.4 * area.getMs() + 0.2 * sex.getMs() );
                        tag.setSs(0.4 * age.getSs() + 0.4 * area.getSs() + 0.2 * sex.getSs() );
                        tag.setXiaoLv(0.4 * age.getXiaoLv() + 0.4 * area.getXiaoLv() + 0.2 * sex.getXiaoLv() );
                        tag.setRt(0.4 * age.getRt() + 0.4 * area.getRt() + 0.2 * sex.getRt() );
                        tag.setPb(0.4 * age.getPb() + 0.4 * area.getPb() + 0.2 * sex.getPb() );
                        tag.setHq(0.4 * age.getHq() + 0.4 * area.getHq() + 0.2 * sex.getHq() );
                        tag.setHeQun(0.4 * age.getHeQun() + 0.4 * area.getHeQun() + 0.2 * sex.getHeQun());
                        tag.setHqxx(0.4 * age.getHqxx() + 0.4 * area.getHqxx() + 0.2 * sex.getHqxx() );
                        tag.setYq(0.4 * age.getYq() + 0.4 * area.getYq() + 0.2 * sex.getYq() );
                        ylyqCoreTagList.add(tag);
                }
            }
        }
        ylyqCoreTagMapper.insertBatch(ylyqCoreTagList);
        redisUtil.lSet("ylyqCoreTag", ylyqCoreTagList);
        log.info("finish==========");
    }

    @Override
    public void refreshCity() {

        QueryWrapper<YlyqCity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("city_flag");
        List<Object> objects = iYlyqCityService.listObjs(queryWrapper);
        Set<Integer> set = new HashSet(objects);

        for (Integer cityFlag : set) {
            if (redisUtil.hasKey("ylyqCity-" + cityFlag)){
                redisUtil.del("ylyqCity-" + cityFlag);
            }
            QueryWrapper<YlyqCity> query = new QueryWrapper<>();
            query.eq("city_flag", cityFlag);
            query.select("city_id", "city_name");
            redisUtil.lSet("ylyqCity-"+cityFlag, iYlyqCityService.list(query));
        }
    }
}
